package ua.kpi.ustymenko.testcase;
import ua.kpi.ustymenko.testcase.answer.Answer;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Answers implements Iterable<Answer> {
    private List<Answer> answers;

    public Answers(List<Answer> answers) {
        this.answers = answers;
    }

    public String getAnswerText(int index) {
        return answers.get(index).getValue();
    }

    public boolean contains(Answer answer) {
        return answers.contains(answer);
    }

    public int size() {
        return answers.size();
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int index = 0; index < answers.size(); index++) {
            String formattedAnswer =
                    String.format("%d. %s \n", (index + 1), answers.get(index));
            builder.append(formattedAnswer);
        }
        return builder.toString();
    }

    @Override
    public Iterator<Answer> iterator() {
        return answers.iterator();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof Answers)) {
            return false;
        }
        Answers ans = (Answers) obj;
        Set<Answer> thisAnswers = new TreeSet<Answer>(this.answers);
        Set<Answer> thatAnswers = new TreeSet<Answer>(ans.answers);

        return thisAnswers.equals(thatAnswers);
    }
}